package com.nlp.solve.issue_e;

import com.alibaba.fastjson.JSONObject;
import com.nlp.model.Article;
import com.nlp.solve.Main;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Issue_E {
    private final Logger logger = LoggerFactory.getLogger(Issue_E.class);
    private static final String inputDir = "F:\\IdeaProjects\\NLP2.0\\src\\test\\tmp\\";

//    public static void main(String[] args) throws IOException {
//        Issue_D issue_d = new Issue_D();
//        File file = new File(inputDir);
//        Map<String, Integer> map = new HashMap<>();
//        issue_d.solve(file,map);
//        System.out.println(map);
//    }

    private void solve(File file, Map<String, Integer> map) throws IOException {
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isDirectory()) {
                solve(f, map);
            } else {
                solve_(f, map);
            }
        }
    }

    private void solve_(File f, Map<String, Integer> map) throws IOException {
        String fn = f.getName();
        if (fn.contains("statistic") || fn.contains("issue") || fn.contains("result") | fn.contains("test")) {
            return;
        }
        logger.info("Resolving: {}", fn);
        List<Article> articles = JSONObject.parseArray(Main.read(f.getAbsolutePath()), Article.class);
        String key = f.getParentFile().getName();
        map.put(key, map.getOrDefault(key, 0) + articles.size());
    }

    public static void main(String[] args) throws IOException {
        Issue_E issue_d = new Issue_E();
        File file = new File(inputDir);
        LinkedList<Article> list = new LinkedList<>();
        issue_d.resolve(file, list);
        issue_d.excel(list);
        System.out.println(list.size());
    }

    private void excel(LinkedList<Article> list) throws IOException {
        logger.info("Exceling... ");
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        int index = 0;
        Row row0 = sheet.createRow(0);
        row0.createCell(0).setCellValue("id");
        row0.createCell(1).setCellValue("标题");
        row0.createCell(2).setCellValue("第一作者国家");
        row0.createCell(3).setCellValue("第一作者城市");

        row0.createCell(4).setCellValue("第二作者国家");
        row0.createCell(5).setCellValue("第二作者城市");

        row0.createCell(6).setCellValue("第三作者国家");
        row0.createCell(7).setCellValue("第三作者城市");
        row0.createCell(8).setCellValue("研究学科");

        for (Article article : list) {
            if (index >= 65535) {
                sheet = workbook.createSheet();
                index = 0;
            }
            Row row = sheet.createRow(++index);
            String title = article.getTI();
            row.createCell(0).setCellValue(index);
            row.createCell(1).setCellValue(title);
            String[] c1 = article.getC1();
            //仅需要获取前三位作者相关信息即可
            int j = 1;
            int i = 0;
            for (i = 0; c1 != null && i < c1.length && i < 3; i++) {
                String[] strings = c1[i].split(",");
                //国家
                String country = strings[strings.length - 1];
                row.createCell(++j).setCellValue(country);
                //城市
                String city = strings[strings.length - 2];
                row.createCell(++j).setCellValue(city);
            }
            while (i++ < 3) {
                ++j;
            }
            String[] sc = article.getSC();
            row.createCell(++j).setCellValue(Arrays.toString(sc));
        }
        logger.info("Finish Exceling...");
        try (DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("./all_articles.xls")))) {
            workbook.write(dataOutputStream);
        }

    }

    private void resolve(File file, List<Article> list) throws IOException {
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isDirectory()) {
                resolve(f, list);
            } else {
                resolve_(f, list);
            }
        }
    }

    private void resolve_(File f, List<Article> list) throws IOException {
        String fn = f.getName();
        if (fn.contains("statistic") || fn.contains("issue") || fn.contains("result") | fn.contains("test")) {
            return;
        }
        logger.info("Resolving: {}", fn);
        List<Article> articles = JSONObject.parseArray(Main.read(f.getAbsolutePath()), Article.class);
        list.addAll(articles);
    }
}
